from django.conf import settings
from django.core.mail import send_mail
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.contrib import messages
from .forms import ContactForm



# Create your views here.
def index(request):
	return render(request, 'base/home.html', {})

# def user_center(request):
# 	return render(request, 'base/user_center.html', {})

def contact(request):
	form = ContactForm(request.POST or None)
	if form.is_valid():
		full_name = form.cleaned_data.get("full_name")
		email = form.cleaned_data.get("email")
		message = form.cleaned_data.get("message")
		#print (email, message, full_name)
		subject = '大学生二手交易网站'
		message = "%s: %s %s"%(
					full_name, 
					email, 
					message
				)

		from_email = settings.EMAIL_HOST_USER
		to_email = [from_email]

		if subject and message and from_email and to_email:
			try:
				send_mail(
					subject, 
					message, 
					from_email,
					to_email, 
					#html_message= some_html_message,
					fail_silently =False
				)
			except BadHeaderError:
				return HttpResponse('Invalid header found.')
			messages.success(request, "感谢您的建议")
			return HttpResponseRedirect('/')
		else:
			return HttpResponse("Make sure all fields are entered and valid")	

	context = {
		"form":form,
	}
	return render(request, "base/contact.html", context)
